/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { XButton } = require("../engine/control/XButton");
const { X2DBeta } = require("../engine/graphics/X2DInterface");
const { Scr } = require("../engine/XDefine");
const { XTools } = require("../engine/XTools");
const { XStat } = require("../engine/XStat");
const { XWindow } = require("../engine/control/XWindow");
const { XInput } = require("../engine/control/XInput");
const { WsClient2 } = require("../engine/ws/WsClient2");
const { Page2SelectEnv } = require("./Page2SelectEnv");
const { PackageTool } = require("./ros_msg_utils/PackageTool");

class Page1Connect extends XWindow {
    constructor() {
        super("Page1Connect", false);

        this.input_ = new XInput({
            prompt: "请输入服务器地址:",
            name: "192.168.169.129:54321",
            x: Scr.logicw / 2,
            y: Scr.logich / 2,
            w: 300,
            h: 20
        })
        this.addUI(this.input_);
        this.addUI(new XButton({
            name: "连接",
            x: Scr.logicw / 2,
            y: Scr.logich / 2 + 50,
            w: 100,
            h: 20,
            onClicked: () => {
                WsClient2.startConnect(this.input_.name_)
            }
        }));
        if (XTools.AUTO_LOGIN) {
            WsClient2.startConnect(XTools.AUTO_LOGIN);
        }
    }
    onDraw() {
        super.onDraw();
        if (WsClient2.isConnected()) {
            XStat.popStat();
            Page2SelectEnv.open();
        }
    }
    onTouch(msg, x, y) {
        if (super.onTouch(msg, x, y)) {
            return true;
        }
        return false;
    }
    onKey(type, k) {
        if (type == 0) {
            return true;
        }
        return false;
    }
    static open() {
        XStat.pushStat(new Page1Connect());
    }
}

module.exports = {
    Page1Connect
}